{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "from tqdm import tqdm\n",
    "import warnings\n",
    "from sklearn.preprocessing import MultiLabelBinarizer\n",
    "warnings.filterwarnings(\"ignore\")#忽略警告"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "导入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((5000, 401), (5000, 1))"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_data=np.hstack((np.ones((5000,1)),np.array(pd.read_csv('X_data.csv',header=None))))#在最左侧加一列全一列\n",
    "y_label=np.array(pd.read_csv('y_label.csv',header=None))\n",
    "y_label=y_label%10\n",
    "x_data.shape,y_label.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.请下载数据给定X_data，y_label，设计一个输入节点个数为400，隐藏层节点个数为25，输出节点个数为10的神经网络。随机产生参数的初始值，计算该初始值对应的梯度。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "sigmoid函数\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sigmoid(x):\n",
    "    return 1/(1+np.exp(-x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建模型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def model(x_data,theta1,theta2,type='train'):\n",
    "    if type!='train' and type!='eval':# 判断模型使用类型是否为训练或验证，如果不是输出错误并返回\n",
    "        print('error')\n",
    "        return\n",
    "    a1=x_data\n",
    "    # 第一层网络\n",
    "    z2=np.dot(x_data,theta1)\n",
    "    a2=sigmoid(z2)\n",
    "    # 第二层网络\n",
    "    a3=np.hstack((np.ones((a2.shape[0],1)),a2))\n",
    "    z3=np.dot(a3,theta2)\n",
    "    h=sigmoid(z3)\n",
    "    if type=='eval':\n",
    "        return np.argmax(h,axis=1)# 如果模型为验证，那么只返回得到的y标签\n",
    "    return a1,z2,a2,a3,z3,h\n",
    "    \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "损失函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def loss(x_data,y_label,theta1,theta2,lamda=0):# 损失函数\n",
    "    _,_,_,_,_,h=model(x_data,theta1,theta2,type='train')\n",
    "    y_label=np.eye(10)[y_label.reshape(-1)]\n",
    "    j=-(np.multiply(y_label,np.log(h))+np.multiply(1-y_label,np.log(1-h))).sum(axis=1).mean()\n",
    "    return j"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "构建梯度函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def gradient(X,y,Theta1,Theta2):\n",
    "    X_1,hidden1,A1,A1_1,out,A2=model(x_data,Theta1,Theta2)\n",
    "    y_onehot=np.eye(10)[y.reshape(-1)]\n",
    "    grad_A2=-1/X.shape[0]*(y_onehot/A2-(1-y_onehot)/(1-A2))\n",
    "\n",
    "    grad_out=grad_A2* -1/(1+np.exp(-out))**2 *np.exp(-out)\n",
    "\n",
    "    grad_Theta2=np.zeros_like(Theta2)\n",
    "    for i in range(grad_Theta2.shape[0]):\n",
    "        grad_Theta2[i]=(grad_out* A1_1[:,i].reshape(-1,1)).sum(axis=0)\n",
    "\n",
    "    grad_A1_1=np.zeros_like(A1_1)\n",
    "    for i in range(grad_A1_1.shape[1]):\n",
    "        grad_A1_1[:,i]=(grad_out* Theta2[i].reshape(1,-1)).sum(axis=1)\n",
    "\n",
    "    grad_A1=grad_A1_1[:,1:]\n",
    "\n",
    "    grad_hidden1=grad_A1* -1/(1+np.exp(-hidden1))**2*np.exp(-hidden1)\n",
    "\n",
    "    grad_Theta1=np.zeros_like(Theta1)\n",
    "    for i in range(grad_Theta1.shape[0]):\n",
    "        grad_Theta1[i]=(grad_hidden1* X_1[:,i].reshape(-1,1)).sum(axis=0)\n",
    "    return grad_Theta1,-1*grad_Theta2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "准确率函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "def acc(y,y_label):#准确率函数\n",
    "    return (y.reshape(-1,1)==y_label).sum()/y.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "随机生成初始theta1和theta2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((401, 25), (26, 10))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta1=np.random.normal(size=[401, 25])#随机生成theta1\n",
    "theta2=np.random.normal(size=[26, 10])#随机生成theta2\n",
    "theta1.shape,theta2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc=9.379999999999999%\n",
      "loss=13.984567100625183\n",
      "grad=(array([[ 2.55968791e-02,  1.06830746e-01,  2.18158816e-01, ...,\n",
      "         7.88111824e-03,  4.05718875e-02,  1.11774281e-01],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
      "       ...,\n",
      "       [ 4.03153740e-11,  2.84648088e-07,  1.09359691e-06, ...,\n",
      "        -1.42574072e-06,  1.03645444e-07, -6.75696416e-09],\n",
      "       [-4.14617416e-12,  4.36165242e-09, -5.33363453e-08, ...,\n",
      "         1.59808739e-07, -6.27310572e-10,  8.85476019e-12],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00]]), array([[-6.60733724e-02,  6.13878450e-01,  4.45063090e-01,\n",
      "         7.40836921e-01, -5.13813186e-02,  8.64011278e-01,\n",
      "         6.02275739e-02,  2.03779051e-01,  5.15785149e-01,\n",
      "         4.32681054e-01],\n",
      "       [-6.81453619e-02,  6.09587142e-01,  4.38583036e-01,\n",
      "         7.29516124e-01, -5.21469050e-02,  8.52009169e-01,\n",
      "         6.71656510e-02,  1.99771383e-01,  5.07243972e-01,\n",
      "         4.24690777e-01],\n",
      "       [-3.82910353e-02,  2.25179809e-01,  1.38876945e-01,\n",
      "         2.42450554e-01, -1.32591116e-02,  2.98788664e-01,\n",
      "         4.41778195e-02,  8.52897602e-02,  1.82323923e-01,\n",
      "         1.91584297e-01],\n",
      "       [-5.65612714e-02,  4.52907978e-01,  3.16226997e-01,\n",
      "         5.74288635e-01, -2.29507266e-02,  6.44391598e-01,\n",
      "         3.04320563e-02,  1.29196349e-01,  4.44813788e-01,\n",
      "         3.24625958e-01],\n",
      "       [-8.29906926e-02,  3.80288307e-01,  2.03727142e-01,\n",
      "         3.41622811e-01, -3.90500843e-02,  4.11083861e-01,\n",
      "         6.47652175e-02,  7.24199550e-02,  2.87482583e-01,\n",
      "         2.80094061e-01],\n",
      "       [ 7.45452178e-03,  1.01479895e-01,  1.43264519e-01,\n",
      "         2.14315682e-01, -5.76809073e-03,  2.19702372e-01,\n",
      "        -3.71395391e-02,  6.65635690e-02,  1.45087292e-01,\n",
      "         7.67075687e-02],\n",
      "       [-6.89551703e-02,  5.08088747e-01,  3.38355836e-01,\n",
      "         5.61124606e-01, -3.39011939e-02,  6.74011549e-01,\n",
      "         6.56189529e-02,  1.74360200e-01,  3.77826381e-01,\n",
      "         3.35258267e-01],\n",
      "       [-6.56895105e-02,  5.80837204e-01,  3.98735448e-01,\n",
      "         6.38977246e-01, -5.83496782e-02,  7.42622632e-01,\n",
      "         5.83352288e-02,  1.47515516e-01,  4.69885663e-01,\n",
      "         3.75195782e-01],\n",
      "       [-3.41552577e-04,  6.54816508e-02,  2.63825078e-02,\n",
      "         3.87280069e-02,  5.22385835e-03,  6.64289971e-02,\n",
      "         5.26677915e-03,  2.23333336e-02,  3.89337309e-02,\n",
      "         4.74048020e-02],\n",
      "       [-4.22985915e-02,  4.57345181e-01,  3.38274532e-01,\n",
      "         5.49304115e-01, -6.82754372e-02,  6.47919452e-01,\n",
      "         5.53121597e-02,  1.57983884e-01,  3.62394212e-01,\n",
      "         3.12645491e-01],\n",
      "       [-7.00420189e-02,  5.72887769e-01,  4.13757170e-01,\n",
      "         6.87704705e-01, -5.73307874e-02,  7.89275712e-01,\n",
      "         7.38958714e-02,  1.77811522e-01,  4.82035745e-01,\n",
      "         3.92258657e-01],\n",
      "       [-6.91263486e-03,  7.34589114e-02,  3.74218097e-02,\n",
      "         7.37214681e-02, -6.83065102e-04,  9.35348826e-02,\n",
      "         2.10745776e-02,  4.61124271e-02,  5.26422862e-02,\n",
      "         6.95480690e-02],\n",
      "       [-7.12437569e-02,  3.42073348e-01,  2.12901202e-01,\n",
      "         4.13898908e-01, -2.41808233e-02,  4.64025094e-01,\n",
      "         5.33522826e-03,  4.57435242e-02,  3.35369704e-01,\n",
      "         2.57839815e-01],\n",
      "       [ 1.57500206e-03, -2.69287589e-03,  3.28393995e-02,\n",
      "         1.12337420e-01,  1.23400225e-02,  1.13916843e-01,\n",
      "        -7.27379144e-04,  5.46329571e-02,  8.42142807e-02,\n",
      "         4.78993080e-02],\n",
      "       [-5.81062653e-02,  4.24149543e-01,  3.33325703e-01,\n",
      "         5.73200279e-01, -3.44940576e-02,  6.70286416e-01,\n",
      "         6.10722913e-02,  1.89185961e-01,  4.14033723e-01,\n",
      "         3.29677020e-01],\n",
      "       [-5.35983878e-02,  5.54966003e-01,  3.97413876e-01,\n",
      "         6.79020296e-01, -6.19325291e-02,  7.76996048e-01,\n",
      "         4.82046783e-02,  1.83109433e-01,  4.68548373e-01,\n",
      "         3.72219259e-01],\n",
      "       [-6.33529640e-02,  2.90736987e-01,  1.34977276e-01,\n",
      "         2.22772057e-01, -1.06169174e-02,  3.09007139e-01,\n",
      "         1.34296873e-02,  6.03924481e-02,  1.61129390e-01,\n",
      "         2.46616017e-01],\n",
      "       [-3.81447557e-02,  3.07027599e-01,  2.34768518e-01,\n",
      "         3.50629985e-01, -2.00538377e-02,  4.27074307e-01,\n",
      "         1.32162198e-02,  5.33673522e-02,  2.75209001e-01,\n",
      "         1.98201465e-01],\n",
      "       [-1.71817169e-02,  1.88496486e-01,  9.67341413e-02,\n",
      "         1.74556983e-01, -1.30338657e-02,  1.99553593e-01,\n",
      "         2.83470795e-02,  4.23182745e-02,  1.11405351e-01,\n",
      "         1.39370901e-01],\n",
      "       [-5.94466608e-04,  3.72603774e-01,  2.80527683e-01,\n",
      "         4.81807767e-01, -4.35069354e-02,  5.62000363e-01,\n",
      "         1.11136552e-02,  1.88838253e-01,  2.53867513e-01,\n",
      "         2.94574324e-01],\n",
      "       [-6.52203539e-02,  6.05759348e-01,  4.37525376e-01,\n",
      "         7.25258671e-01, -5.22652222e-02,  8.49035572e-01,\n",
      "         5.94200853e-02,  2.01292327e-01,  5.02530186e-01,\n",
      "         4.24393382e-01],\n",
      "       [-4.70691804e-02,  3.38312241e-01,  1.79620798e-01,\n",
      "         3.09845295e-01, -5.09596360e-02,  3.46021521e-01,\n",
      "        -3.25663509e-03,  4.77020612e-02,  2.45999696e-01,\n",
      "         2.39278953e-01],\n",
      "       [ 8.33622341e-03,  1.33854248e-01,  1.48075471e-01,\n",
      "         1.78589019e-01, -4.23810639e-02,  1.74832062e-01,\n",
      "         1.65501888e-02,  3.42673503e-02,  1.52023641e-01,\n",
      "         5.08114317e-02],\n",
      "       [ 1.08772888e-03,  2.47156843e-01,  2.64816766e-01,\n",
      "         4.72053773e-01, -1.57849981e-02,  4.96965635e-01,\n",
      "        -4.38086429e-03,  1.67877356e-01,  2.50529616e-01,\n",
      "         2.01518299e-01],\n",
      "       [-5.14216430e-02,  4.59779064e-01,  3.70571858e-01,\n",
      "         6.33950177e-01, -3.07116941e-02,  7.07701113e-01,\n",
      "         3.28710579e-02,  2.18919406e-01,  4.01943186e-01,\n",
      "         3.36328544e-01],\n",
      "       [-1.54863517e-04,  9.83172217e-02,  9.03807866e-02,\n",
      "         1.40207355e-01, -1.67722123e-02,  1.64154207e-01,\n",
      "         2.56862149e-02,  5.28528473e-02,  1.19476203e-01,\n",
      "         5.23022045e-02]]))\n"
     ]
    }
   ],
   "source": [
    "y=model(x_data,theta1=theta1,theta2=theta2,type='eval')#将数据和初始参数放入模型\n",
    "\n",
    "print(f'acc={acc(y,y_label)*100}%')\n",
    "print(f'loss={loss(x_data,y_label,theta1,theta2)}')\n",
    "grad1,grad2=gradient(x_data,y_label,theta1,theta2)\n",
    "print(f'grad={grad1,grad2}')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.对第1题产生的随机参数初始值添加一个很小的扰动(0.0001)，计算该初始值对应的梯度的近似值并与第1题的计算结果相比较，请列出两者的误差。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acc=9.379999999999999%\n",
      "loss=13.984567100625183\n",
      "grad=(array([[ 2.55528441e-02,  1.06878870e-01,  2.18092546e-01, ...,\n",
      "         7.76308469e-03,  4.05473758e-02,  1.11954707e-01],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00],\n",
      "       ...,\n",
      "       [ 4.05613598e-11,  2.84539357e-07,  1.08783560e-06, ...,\n",
      "        -1.42329711e-06,  1.03566515e-07, -6.80522229e-09],\n",
      "       [-4.12283196e-12,  4.35287042e-09, -5.29117572e-08, ...,\n",
      "         1.59486296e-07, -6.11478945e-10,  9.03523451e-12],\n",
      "       [ 0.00000000e+00,  0.00000000e+00,  0.00000000e+00, ...,\n",
      "         0.00000000e+00,  0.00000000e+00,  0.00000000e+00]]), array([[-6.60341424e-02,  6.14041302e-01,  4.45031383e-01,\n",
      "         7.41057830e-01, -5.13474424e-02,  8.64059992e-01,\n",
      "         6.02203322e-02,  2.03933686e-01,  5.16193363e-01,\n",
      "         4.33136833e-01],\n",
      "       [-6.81043676e-02,  6.09759618e-01,  4.38565462e-01,\n",
      "         7.29761186e-01, -5.21119927e-02,  8.52085545e-01,\n",
      "         6.71418009e-02,  1.99932978e-01,  5.07665827e-01,\n",
      "         4.25159367e-01],\n",
      "       [-3.83303999e-02,  2.25493216e-01,  1.38969497e-01,\n",
      "         2.42794924e-01, -1.32614756e-02,  2.99104587e-01,\n",
      "         4.41926309e-02,  8.54405851e-02,  1.82633022e-01,\n",
      "         1.91970007e-01],\n",
      "       [-5.65536473e-02,  4.53301026e-01,  3.16435517e-01,\n",
      "         5.74727818e-01, -2.29561909e-02,  6.44777744e-01,\n",
      "         3.04685706e-02,  1.29430294e-01,  4.45245006e-01,\n",
      "         3.25143874e-01],\n",
      "       [-8.29789813e-02,  3.80583887e-01,  2.03880974e-01,\n",
      "         3.41994649e-01, -3.90604985e-02,  4.11375574e-01,\n",
      "         6.47607161e-02,  7.25655440e-02,  2.87801986e-01,\n",
      "         2.80465096e-01],\n",
      "       [ 7.46918069e-03,  1.01704030e-01,  1.43437781e-01,\n",
      "         2.14589960e-01, -5.77240895e-03,  2.19973730e-01,\n",
      "        -3.71535344e-02,  6.66675220e-02,  1.45365188e-01,\n",
      "         7.69169131e-02],\n",
      "       [-6.89421601e-02,  5.08379880e-01,  3.38469361e-01,\n",
      "         5.61560295e-01, -3.38956316e-02,  6.74329096e-01,\n",
      "         6.56174114e-02,  1.74531814e-01,  3.78340062e-01,\n",
      "         3.35764135e-01],\n",
      "       [-6.56566369e-02,  5.81091559e-01,  3.98818581e-01,\n",
      "         6.39347296e-01, -5.83184141e-02,  7.42876219e-01,\n",
      "         5.83247726e-02,  1.47731245e-01,  4.70299671e-01,\n",
      "         3.75702396e-01],\n",
      "       [-3.43075682e-04,  6.56049771e-02,  2.64095031e-02,\n",
      "         3.88316340e-02,  5.23904463e-03,  6.65536378e-02,\n",
      "         5.27696991e-03,  2.23890957e-02,  3.90420041e-02,\n",
      "         4.75300610e-02],\n",
      "       [-4.23116108e-02,  4.57730518e-01,  3.38431242e-01,\n",
      "         5.49732425e-01, -6.82517130e-02,  6.48263612e-01,\n",
      "         5.53170171e-02,  1.58165963e-01,  3.62910058e-01,\n",
      "         3.13162500e-01],\n",
      "       [-7.00065722e-02,  5.73111291e-01,  4.13802870e-01,\n",
      "         6.88002862e-01, -5.72917316e-02,  7.89451476e-01,\n",
      "         7.38697163e-02,  1.78003829e-01,  4.82469074e-01,\n",
      "         3.92753326e-01],\n",
      "       [-6.93536909e-03,  7.36250120e-02,  3.74807740e-02,\n",
      "         7.38767781e-02, -6.81482135e-04,  9.37037207e-02,\n",
      "         2.11219023e-02,  4.62051989e-02,  5.27742927e-02,\n",
      "         6.97282444e-02],\n",
      "       [-7.12420305e-02,  3.42412362e-01,  2.13070274e-01,\n",
      "         4.14320030e-01, -2.41930781e-02,  4.64416302e-01,\n",
      "         5.33972538e-03,  4.58992283e-02,  3.35774571e-01,\n",
      "         2.58250698e-01],\n",
      "       [ 1.57865683e-03, -2.60599628e-03,  3.28634404e-02,\n",
      "         1.12467209e-01,  1.23544385e-02,  1.14055090e-01,\n",
      "        -7.29474270e-04,  5.47087294e-02,  8.43451224e-02,\n",
      "         4.80128058e-02],\n",
      "       [-5.81098045e-02,  4.24596382e-01,  3.33467778e-01,\n",
      "         5.73632878e-01, -3.44863126e-02,  6.70649819e-01,\n",
      "         6.10627208e-02,  1.89363733e-01,  4.14508161e-01,\n",
      "         3.30215091e-01],\n",
      "       [-5.35923369e-02,  5.55251111e-01,  3.97499350e-01,\n",
      "         6.79325415e-01, -6.18975584e-02,  7.77200637e-01,\n",
      "         4.82271685e-02,  1.83283212e-01,  4.68996052e-01,\n",
      "         3.72748305e-01],\n",
      "       [-6.33766763e-02,  2.91071706e-01,  1.35113381e-01,\n",
      "         2.23178754e-01, -1.06299389e-02,  3.09360462e-01,\n",
      "         1.34486836e-02,  6.05153907e-02,  1.61471789e-01,\n",
      "         2.46980275e-01],\n",
      "       [-3.81855476e-02,  3.07440990e-01,  2.34998888e-01,\n",
      "         3.51160193e-01, -2.00607340e-02,  4.27569988e-01,\n",
      "         1.32413081e-02,  5.35364893e-02,  2.75711197e-01,\n",
      "         1.98664081e-01],\n",
      "       [-1.72064402e-02,  1.88824847e-01,  9.68959837e-02,\n",
      "         1.74884460e-01, -1.30550680e-02,  1.99890114e-01,\n",
      "         2.83844400e-02,  4.24154361e-02,  1.11695907e-01,\n",
      "         1.39688279e-01],\n",
      "       [-6.09966786e-04,  3.72975280e-01,  2.80699460e-01,\n",
      "         4.82223181e-01, -4.34998809e-02,  5.62378602e-01,\n",
      "         1.11663151e-02,  1.89003474e-01,  2.54398202e-01,\n",
      "         2.95010336e-01],\n",
      "       [-6.51832817e-02,  6.05942794e-01,  4.37508587e-01,\n",
      "         7.25512691e-01, -5.22303120e-02,  8.49116649e-01,\n",
      "         5.94147095e-02,  2.01447420e-01,  5.02962410e-01,\n",
      "         4.24862438e-01],\n",
      "       [-4.70985689e-02,  3.38674807e-01,  1.79788972e-01,\n",
      "         3.10266351e-01, -5.09785005e-02,  3.46394811e-01,\n",
      "        -3.21601122e-03,  4.78160080e-02,  2.46386464e-01,\n",
      "         2.39660905e-01],\n",
      "       [ 8.35556719e-03,  1.34042102e-01,  1.48235506e-01,\n",
      "         1.78802202e-01, -4.24057318e-02,  1.75022643e-01,\n",
      "         1.65425150e-02,  3.43400431e-02,  1.52227289e-01,\n",
      "         5.09840312e-02],\n",
      "       [ 1.08349792e-03,  2.47585637e-01,  2.64997123e-01,\n",
      "         4.72450592e-01, -1.57953930e-02,  4.97339048e-01,\n",
      "        -4.34412880e-03,  1.68045618e-01,  2.50978768e-01,\n",
      "         2.01957939e-01],\n",
      "       [-5.14157959e-02,  4.60122366e-01,  3.70644131e-01,\n",
      "         6.34249212e-01, -3.07003139e-02,  7.07945067e-01,\n",
      "         3.29074645e-02,  2.19064015e-01,  4.02418603e-01,\n",
      "         3.36827021e-01],\n",
      "       [-1.55668714e-04,  9.85118846e-02,  9.05128653e-02,\n",
      "         1.40446617e-01, -1.67818702e-02,  1.64404677e-01,\n",
      "         2.57201788e-02,  5.29666551e-02,  1.19706743e-01,\n",
      "         5.24917536e-02]]))\n"
     ]
    }
   ],
   "source": [
    "y=model(x_data,theta1=theta1,theta2=theta2,type='eval')#将数据和初始参数放入模型\n",
    "print(f'acc={acc(y,y_label)*100}%')\n",
    "print(f'loss={loss(x_data,y_label,theta1,theta2)}')\n",
    "grad1_1,grad2_1=gradient(x_data,y_label,theta1+0.0001,theta2+0.0001)\n",
    "print(f'grad={grad1_1,grad2_1}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "grad1:-1.3572862983986884e-06\n",
      "grad2:-0.0001905765738880423\n"
     ]
    }
   ],
   "source": [
    "#求梯度误差的平均值\n",
    "grad1_avg=np.sum(grad1-grad1_1)/(grad1_1.shape[0]*grad1_1.shape[1])\n",
    "print(f'grad1:{grad1_avg}')\n",
    "grad2_avg=np.sum(grad2-grad2_1)/(grad2_1.shape[0]*grad2_1.shape[1])\n",
    "print(f'grad2:{grad2_avg}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-4.18039223e-07 -1.78247582e-06  2.12071404e-05  3.98247432e-05\n",
      "  5.22165878e-06  4.05201116e-07 -5.46024272e-07 -3.91091460e-06\n",
      " -3.19880023e-06  2.16079942e-05  1.49381555e-06  6.72664060e-06\n",
      " -1.66191232e-05 -2.72919273e-06 -3.85593822e-08  3.58024739e-06\n",
      " -3.18119149e-05  5.82490226e-06  2.47062576e-06 -3.24185875e-07\n",
      "  2.77461944e-05  3.35654491e-06 -1.05146553e-05  1.78750683e-06\n",
      " -4.19992718e-06]\n",
      "[-4.16927534e-07 -1.78665560e-06  2.12069314e-05  3.98812976e-05\n",
      "  5.21982181e-06  4.04257189e-07 -5.41884477e-07 -3.91739620e-06\n",
      " -3.19890478e-06  2.16848048e-05  1.50699690e-06  6.71129611e-06\n",
      " -1.66562205e-05 -2.71783009e-06 -3.82457600e-08  3.60551069e-06\n",
      " -3.18606889e-05  5.83020157e-06  2.46672151e-06 -3.20872594e-07\n",
      "  2.77910544e-05  3.36455950e-06 -1.05352325e-05  1.77756751e-06\n",
      " -4.21066236e-06]\n"
     ]
    }
   ],
   "source": [
    "aaa=10\n",
    "print(grad1[aaa])\n",
    "print(grad1_1[aaa])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3.利用梯度下降法求解神经网络的参数，并给出神经网络的分类准确率。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(x,y,theta1,theta2,lr,epoch=100):\n",
    "    e=[]\n",
    "    loss_s=[]\n",
    "    acc_s=[]\n",
    "    #梯度下降求解\n",
    "    for i in tqdm(range(epoch)):\n",
    "        grad1,grad2 = gradient(x,y,theta1,theta2)\n",
    "        theta1 = theta1 - lr*grad1 # 参数更新\n",
    "        theta2 = theta2 - lr*grad2 # 参数更新\n",
    "        loss_1=loss(x_data,y_label,theta1,theta2)# 计算新的损失\n",
    "        h=model(x,theta1,theta2,type='eval')\n",
    "        acc_1=acc(h,y)# 计算准确率\n",
    "        loss_s.append(loss_1) \n",
    "        acc_s.append(acc_1)\n",
    "        e.append(i+1)\n",
    "        if np.isnan(loss_1):\n",
    "            break\n",
    "        if ((i+1)%10==0 and lr>0.01):#lr大于0.01时，每10个epoch减小一次学习率\n",
    "            lr=lr*0.99 \n",
    "        #     print(f'epoch {i+1} : loss={loss_1},Acc sorce={acc_1}')\n",
    "    print(f'Fanily Loss: {loss_s[-1]},Acc sorce={acc_s[-1]*100}')\n",
    "    plt.plot(e,loss_s)\n",
    "    plt.show()\n",
    "    plt.plot(e,acc_s)\n",
    "    plt.show()\n",
    "    return theta1,theta2,e,loss_s,acc_s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "((401, 25), (26, 10))"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "theta1=np.random.normal(size=[401, 25])#随机生成theta1\n",
    "theta2=np.random.normal(size=[26, 10])#随机生成theta2\n",
    "# theta1=np.random.randn(401,25)\n",
    "# theta2=np.random.randn(26,10)\n",
    "theta1.shape,theta2.shape\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 3000/3000 [09:41<00:00,  5.16it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fanily Loss: 0.35238927228068995,Acc sorce=95.78\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXAAAAD4CAYAAAD1jb0+AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8/fFQqAAAACXBIWXMAAAsTAAALE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uX5fa+TLIvWb4vETERERtU3JHsHElnLOT2cwjw7G7dFhH70uOopK+peGMP1LtG0uNoWj2H/Ztp7Yt2nyLiQPpPV5P0KTX/VF3U+2J7UEXgfS4ivpqas3xfJtuXXN+Xuoh4WtI2SW9Q0WVVv9dCa12NmtPyl0n6ueawLzkEeFa3brO93PbK+rSkCyXtVFFzfdR/o6Tb0vTtkn4vnTlwrqRnWv4sXixmWvu3JF1oe1X6U/jC1NZ3HeMLl6l4b6RiXy5PZwqsl3S6pHu1CH7/Uj/pLZJ2R8RNLYuye1+m2pdM35ch2yel6RMkXaCiT3+bpHen1Trfl/r79W5J/5n+cppqH7tbyFHb2f6oGFV/REX/0of7XU+XWk9TMaJ8v6Rd9XpV9HXdJelRSd+RdHI0R7I/nvbtQUnDfa7/8yr+hD2ioi/uytnULun9KgZj9kh63yLal8+mWh9I/3HWtqz/4bQvD0t6x2L5/ZN0norukQck7Ug/F+f4vhxlX3J8X14n6Uep5p2Srk/tp6kI4D2S/l3S0tS+LM3vSctP67aP3X64lB4AMpVDFwoAYBIEOABkigAHgEwR4ACQKQIcADJFgANApghwAMjU/wO8xHJ1kirMUAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAD8CAYAAABn919SAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMCwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8/fFQqAAAACXBIWXMAAAsTAAALEwEAmpwYAAAYM0lEQVR4nO3de5QcZ3nn8e/Tt5mRZnSxNbKNLpaMxUUEr3EGm8TgADZG9m6shCVZm+RAAhudJJiQE5KsCVnDmixnY3b5I+c4ceyEE5LN2jGEBG0QKxxHWbLBNpbxVVIEY2EiCSPJsq37zHR3PftHVY967i2pp6vrrd/nnGGq3qqpel5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czKxMH4F+6+5eT5kxel+n6ktXr0uDurwDbgB8jnuIqTVPXeM3J9sXAYc6xL1kL9MeAdcmd4wrxzYTNKdc0IzNbaGYDjWXgeuBZ4pobryr4APCVZHkz8P7klQlvAY40/RndLc609q3A9Wa2NPnT+fqkLXWT7k/8NPG1gbgvNyevRFgLrAO+RRf8/iXzrH8K7HL3zzVtytx1makvGb0ug2a2JFnuA95FfE9gG/DeZLfJ16Vxvd4L/EPyl9VMfWxNJ+8Et+OL+K79d4jnpz6Rdj1z1HoJ8R3rp4AdjXqJ58oeAr4L/D1wnp++U35X0rdngKGU67+P+E/eKvFc3ofOpnbgg8Q3d4aBX+yivvxFUuvTyT+ki5r2/0TSl93ADd3y+we8lXg65WngyeTrxixel1n6ksXrchnwRFLzs8DtSfslxIE8DHwR6Enae5P14WT7JXP1sZUvvVNURCQQWZtyERGRGSjQRUQCoUAXEQmEAl1EJBAKdBGRQCjQRUQCoUAXEQmEAl1EJBD/H+a6bO2ehdVmAAAAAElFTkSuQmCC",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "theta1,theta2,e,loss_s,acc_s=train(x_data,y_label,theta1,theta2,10,epoch=3000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt = pd.DataFrame(theta1, columns=None)# 保存参数\n",
    "dt.to_csv('theta1.csv',index=False)\n",
    "dt = pd.DataFrame(theta2, columns=None)\n",
    "dt.to_csv('theta2.csv',index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "0ea2ea13f89004d253f237dbfddddbdca6ade9e7c69d0cba8f71d034288dfba7"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 64-bit ('tensorflow': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
